//
//  UmengTrackObject.m
//  duzhoumo
//
//  Created by dzmmac on 15/7/13.
//  Copyright (c) 2015年 dzmmac. All rights reserved.
//

#import "UmengTrackObject.h"
#include <sys/socket.h>
#include <sys/sysctl.h>
#include <net/if.h>
#include <net/if_dl.h>
#import  <AdSupport/AdSupport.h>
#import "UIDevice+IdentifierAddition.h"
#import "UMSocialQQHandler.h"
#import "UMSocialSinaHandler.h"
#import "UMSocialWechatHandler.h"
#import "UMSocialData.h"
#import "UMSocialSnsService.h"
#import "MobClick.h"
#import "WXApi.h"
#import "NSUserDefaults+Addition.h"
#import "AppConfig.h"
#import "UMSocialConfig.h"
#import "AppColorConfig.h"
@implementation UmengTrackObject

+ (UmengTrackObject *) sharedManager
{
    static UmengTrackObject *sharedManagerInstance = nil;
    static dispatch_once_t predicate;
    dispatch_once(&predicate, ^{
        sharedManagerInstance = [[self alloc] init];
    });
    return sharedManagerInstance;
}

-(void)startTrack{
    NSString * appKey = @"5423840ffd98c5857506f00d";
    NSString * deviceName = [[[UIDevice currentDevice] name] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSString * mac =  [UIDevice macString];
    NSString * idfa = [UIDevice getIdfa];
    NSString * idfv = [[UIDevice currentDevice] idfvString];
    NSString * urlString = [NSString stringWithFormat:@"http://log.umtrack.com/ping/%@/?devicename=%@&mac=%@&idfa=%@&idfv=%@", appKey, deviceName, mac, idfa, idfv];
    [NSURLConnection connectionWithRequest:[NSURLRequest requestWithURL: [NSURL URLWithString:urlString]] delegate:nil];
}


 // 配置友盟
-(void)umengShareConfig{

    // 分享授权样式配置
    [UMSocialConfig setTheme:UMSocialThemeBlack];

    //设置友盟社会化组件appkey
    [UMSocialData setAppKey:@"5423840ffd98c5857506f00d"];
    //设置微信AppId，设置分享url，默认使用友盟的网址
    [UMSocialWechatHandler setWXAppId:@"wx4526f14af4968b61" appSecret:@"4aca13c7cc2dd221722ff421070d8200" url:@"http://www.duzhoumo.com"];
    
    //设置分享到QQ空间的应用Id，和分享url 链接
    [UMSocialQQHandler setQQWithAppId:@"1103293657" appKey:@"bSZwj5fCQObL3p0b" url:@"http://www.duzhoumo.com"];
    
    [UMSocialSinaHandler openSSOWithRedirectURL:@"http://sns.whalecloud.com/sina2/callback"];
    
    //设置支持没有客户端情况下使用SSO授权
    [UMSocialQQHandler setSupportWebView:YES];
    
    [MobClick setAppVersion:XcodeAppVersion];
    
    [MobClick setCrashReportEnabled:YES];
}

/**
 *  友盟在线更新
 */
-(void)umengUpdateConfig{
    
    [MobClick checkUpdateWithDelegate:self selector:@selector(appUpdate:)];
    [MobClick startWithAppkey:@"5423840ffd98c5857506f00d" reportPolicy:(ReportPolicy) REALTIME channelId:nil];
}

- (void)appUpdate:(NSDictionary *)appUpdateInfo{
    NSString *currentVersionStr = [appUpdateInfo objectForKey:@"current_version"];
    NSString *versionStr = [appUpdateInfo objectForKey:@"version"] ;
    currentVersionStr =  [currentVersionStr stringByReplacingOccurrencesOfString:@"." withString:@""];
    versionStr =  [versionStr stringByReplacingOccurrencesOfString:@"." withString:@""];
    int currentVersion = [currentVersionStr intValue];
    int version = [versionStr intValue];
    self.appUpdatePath = appUpdateInfo[@"path"];
    // 有版本更新
    if (version > currentVersion) {
        [[NSUserDefaults standardUserDefaults] app_setLaunched:NO];
        UIAlertView *alert =  [ [UIAlertView alloc ] initWithTitle:@"新版本" message:[appUpdateInfo objectForKey:@"update_log" ] delegate:self cancelButtonTitle:@"忽略" otherButtonTitles:@"更新", nil] ;
        [alert show];
    }
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (buttonIndex == 1) {
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:self.appUpdatePath]];
    }
}

@end
